package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.BigDecimalPropertyEditor;
import com.feeyin.radar.commons.DatePropertyEditor;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.crm.model.QueryCustomer;
import com.feeyin.radar.crm.service.ContractCustomerService;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.sys.service.DepartmentService;
import com.feeyin.radar.sys.service.UserService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class ContractCustomerController extends MultiActionController
{
  private ContractCustomerService contractCustomerService;
  private CustomerService customerService;
  private UserService userService;
  private DepartmentService departmentService;
  private String contractCustomer;
  private String deptContractCustomer;
  private String advancedQueryDialog1;

  public UserService getUserService()
  {
    return this.userService;
  }

  public void setUserService(UserService userService) {
    this.userService = userService;
  }

  public DepartmentService getDepartmentService() {
    return this.departmentService;
  }

  public void setDepartmentService(DepartmentService departmentService) {
    this.departmentService = departmentService;
  }

  public String getAdvancedQueryDialog1()
  {
    return this.advancedQueryDialog1;
  }

  public void setAdvancedQueryDialog1(String advancedQueryDialog1) {
    this.advancedQueryDialog1 = advancedQueryDialog1;
  }

  public ContractCustomerService getContractCustomerService() {
    return this.contractCustomerService;
  }

  public void setContractCustomerService(ContractCustomerService contractCustomerService)
  {
    this.contractCustomerService = contractCustomerService;
  }

  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  public String getContractCustomer() {
    return this.contractCustomer;
  }

  public void setContractCustomer(String contractCustomer) {
    this.contractCustomer = contractCustomer;
  }

  public String getDeptContractCustomer() {
    return this.deptContractCustomer;
  }

  public void setDeptContractCustomer(String deptContractCustomer) {
    this.deptContractCustomer = deptContractCustomer;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response, QueryCustomer queryCustomer)
  {
    Map map = new HashMap();
    map.put("queryCustomer", queryCustomer);
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    map.put("deleteResult", request.getParameter("deleteResult"));
    Page page = new Page();
    page.getPageList(this.contractCustomerService.getPageContractCustomersCount(userId, queryCustomer).intValue(), null, request);
    map.put("customers", this.contractCustomerService.getPageContractCustomers(userId, page, queryCustomer));
    map.put("ranges", this.customerService.getCustomerDictionaryRanges());

    return new ModelAndView(getContractCustomer(), map);
  }

  public ModelAndView deptCustomer(HttpServletRequest request, HttpServletResponse response, QueryCustomer queryCustomer)
  {
    Map map = new HashMap();
    map.put("queryCustomer", queryCustomer);
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    map.put("deleteResult", request.getParameter("deleteResult"));
    Page page = new Page();
    page.getPageList(this.contractCustomerService.getPageDeptContractCustomersCount(userId, queryCustomer).intValue(), null, request);
    map.put("customers", this.contractCustomerService.getPageDeptContractCustomers(userId, page, queryCustomer));
    map.put("ranges", this.customerService.getCustomerDictionaryRanges());

    return new ModelAndView(getDeptContractCustomer(), map);
  }

  public ModelAndView advancedQueryDialog1(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    List users = new ArrayList();

    if (userId != null) {
      String deptIds = this.departmentService.getCurrentDeptIdAndSubDeptIdsByUserId(userId);
      users = this.userService.getActivityUsersByDeptIds(deptIds);
    }
    map.put("users", users);
    map.put("records", this.customerService.getCustomerInfoRecords());
    map.put("ranges", this.customerService.getCustomerDictionaryRanges());
    return new ModelAndView(getAdvancedQueryDialog1(), map);
  }

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    binder.registerCustomEditor(Date.class, new DatePropertyEditor());
    binder.registerCustomEditor(BigDecimal.class, new BigDecimalPropertyEditor());
  }
}